


$(function () {
    /**
     *  解析url  的参数
     */
    function  queryURLparams() {
        var url = window.location.href;
        var obj = {}
        if (url.indexOf('?') < 0) return obj
        var arr = url.split('?')
        url = arr[1]
        var array = url.split('&')
        for (var i = 0; i < array.length; i++) {
            var arr2 = array[i]
            var arr3 = arr2.split('=')
            obj[arr3[0]] = arr3[1]
        }
        console.log(obj);
        return obj
    }
    var  data=queryURLparams(url);
    vm.scheduleId=data.id;
    vm.laboratoryId=data.laboratoryId;
    vm.getLaboratory();
    vm.getJxcourseScheduleById();
    var view=data.view;
    if(view=="0"){
        vm.getDept();    //  拿到所有的班级
     vm.showList=true;
     vm.getAllDevices();
    }else { // 拿到所有的学生
     vm.showList=false;
     vm.getAllDevices();
     vm.getStudentSchedules();
    }
    vm.getDept();    //  拿到所有的班级
//拿到所有的设备
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            name: null
        },
        scheduleId:"",  // 课程预定id
        laboratoryId:"", // 实验室id
        laboratoryName:"",  //  实验室名称
        showList: true,
        hasStudent:false,
        title: null,
        jxStudent: {classId: 0, schoolId: 0,className:"请选择学校及班级"},
        devices:"",  //  所有设备
        students:"",
        className:"",  //   当前班级
        studentScheduleList:[], //  需要上传的数据
        dataList:[], // 界面显示；
        lastData:[{"name":""}], // 剩余未分配的学生；
        params:{"laboratoryId":null,"courseDay":null,"teacherId":null,"courseId":null,"batch":null,"classId":null},   //  上传参数
        treeData: {},
        parentNames: {}, // 所有父级名称
        parentIds: {}, //  所有父级id
        treeMap:{} // 所有的树id-name

    },
    methods: {
        query: function () {
            vm.reload();
        },getAllDevices:function(){   //  拿到所有设备内
            var  url="sys/jxcomputydevice/allDevice";
            vm.params.laboratoryId=vm.laboratoryId;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType : 'application/x-www-form-urlencoded',
                data: vm.params,
                async:false,
                success: function(r){
                    if(r.code === 0){
                    vm.devices=r.devices;
                    for(var i=0;i<vm.devices.length;i++){
                        var schedule={};
                        var showData={};
                        schedule.laboratoryId=vm.laboratoryId;
                        schedule.useDeviceId=vm.devices[i].id; //  设备id
                        schedule.courseScheduleId=vm.scheduleId; //  课程id
                        schedule.studyStatus="0";   //未开始
                        showData.deviceName=vm.devices[i].deviceName;
                        showData.studentName="暂未选定";
                        vm.studentScheduleList.push(schedule);
                        vm.dataList.push(showData);
                    }
                    }
                }
            });
        },getAllStudents:function(){   //  拿到所有未分配的学生
            var  url="sys/jxstudentschedule//lastStudent";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType : 'application/x-www-form-urlencoded',
                data: vm.params,
                async:false,
                success: function(r){
                    console.log(r);
                    if(r.code === 0){
                        vm.students=r.data;
                        var len=vm.devices.length;
                        for(var i=0;i<vm.students.length;i++){
                            if(i<vm.studentScheduleList.length){
                                vm.studentScheduleList[i].studentId=vm.students[i].id;
                                vm.dataList[i].studentName=vm.students[i].name;
                                vm.dataList[i].className=vm.className;
                            }else{
                                vm.lastData[i-len]=vm.students[i];
                            }

                        }
                        if(vm.students.length>len){
                           vm.hasStudent=true;
                        }
                    }
                }
            });

        },getJxcourseScheduleById:function(){    //  拿到当前的实验室预定
            $.get(baseURL + "sys/jxcourseschedule/info/" + vm.scheduleId, function (r) {
                vm.jxCourseSchedule = r.jxCourseSchedule;
                vm.params.laboratoryId=r.jxCourseSchedule.laboratoryId;
                vm.params.courseDay=r.jxCourseSchedule.courseDay;
                vm.params.teacherId=r.jxCourseSchedule.teacherId;
                vm.params.courseId=r.jxCourseSchedule.courseId;
                vm.params.batch=r.jxCourseSchedule.batch;

            });
        },getStudentSchedules:function(){   //  拿到当前的预定信息展示
            var  url="sys/jxstudentschedule/listByPo";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType : 'application/x-www-form-urlencoded',
                data: {"courseScheduleId":vm.scheduleId},
                async:false,
                success: function(r){
                    if(r.code === 0){
                        var list=r.data;
                        var key_val={};
                        console.log(list);
                        console.log(vm.dataList);
                        for(var i=0;i<list.length;i++){
                            var d=list[i];
                            key_val[d.deviceName]=d;
                        }
                        var li=[];
                        vm.dataList.forEach(function(v,i,arry){
                            var deviceName=v.deviceName;
                            if(key_val[deviceName]!=undefined){
                                v=key_val[deviceName];
                            }
                            li.push(v);
                        })
                        vm.dataList=li;
                    }
                }
            });
        }
        , deptTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    console.log(node);
                    //选择上级部门
                    vm.jxStudent.classId = node[0].deptId;
                    var classId = node[0].deptId;
                    vm.jxStudent.className = vm.parentNames[classId]+"|"+vm.treeMap[classId];
                    var schoolId = vm.getFirstText(vm.parentIds[classId]);
                    vm.jxStudent.schoolId = schoolId;
                    $("#schoolwithclass").val(vm.jxStudent.className);
                    vm.params.classId=classId;
                    vm.className=vm.treeMap[classId];
                    vm.getAllStudents();
                    layer.close(index);
                }
            });
        }, getDept: function () {
            //加载部门树
            $.get(baseURL + "sys/dept/select", function (r) {
                console.log(r);
                var first_deptId = r.deptList[0].deptId;   //  第一个id
                ztree = $.fn.zTree.init($("#deptTree"), setting, r.deptList);
                var node = ztree.getNodeByParam("deptId", 0);
                node = (node != null ? node : ztree.getNodeByParam("deptId", first_deptId));
                ztree.selectNode(node);
                vm.jxStudent.className = "请选择相应的班级";
                vm.treeData = node;
                vm.getAllParent(vm.treeData);  //  得到所有的父级数据
                            })
        }, getAllParent: function (data) {  //  得到所有的父级数据
            vm.treeMap[data.deptId]=data.name;
            if (data.deptId != 0 && data.level == 0) { //  学校
                vm.parentNames[data.deptId] = "";
                vm.parentIds[data.deptId] = 0;
            }
            var ch = data.children;
            if (ch.length > 0) {
                ch.forEach(function (child) {
                    vm.treeMap[child.deptId]=child.name;
                    var pid = data.deptId; //  父级id
                    var pName = data.name; //  父级名称
                    var pPnames = vm.parentNames[pid]; // 父级上一级名称；
                    var pPIds = vm.parentIds[pid];//  父级的上以及id;
                    var dptId = child.deptId;  // 当前id
                    if (pPnames != undefined) {
                        vm.parentNames[dptId] = pPnames.length > 0 ? pPnames + "|" + data.name : data.name;
                    } else {
                        vm.parentNames[dptId] = data.level > 0 ? data.name : "";
                    }
                    if (pPIds != undefined) {
                        vm.parentIds[dptId] = pPIds + "|" + data.deptId;
                    } else {
                        vm.parentIds[dptId] = data.level > 0 ? data.deptId : "";
                    }
                    if (child.children != undefined) {
                        vm.getAllParent(child);
                    }

                })

            }
        }, getFirstText: function (text) {
            var txts = text.split("|");
            return parseInt(txts[1]);
        },submit:function(){   // 提交对象
            var  url="sys/jxstudentschedule/submit";
            $.ajax({
                url:baseURL +url,
                type:'post',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data:JSON.stringify(vm.studentScheduleList),
                success:function(data)
                {
                     if(data.code==0){
                    layer.msg("分配成功！");
                    layer.alert('方案已经执行！', {
                        skin: 'layui-layer-molv' //样式类名  自定义样式
                         ,closeBtn: 1    // 是否显示关闭按钮
                         ,anim: 1 //动画类型
                        ,btn: ['知道了！'] //按钮
                       ,icon: 6    // icon
                       ,yes:function(){
                            window.location.href = "jxcourseschedule.html";
                      }
                      });
                }else{
                         layer.msg(data.msg);
                     }
                }
            });
        },getLaboratory : function(){   // 拿到当前实验室
                $.get(baseURL + "sys/jxlaboratory/info/" + vm.laboratoryId, function (r) {
                    vm.laboratoryName= r.jxLaboratory.name;
                });
        }

    }
});